<?php
namespace Admin\Controller;
use Org\Util\Tree;

class UserController extends AdminController {
    
    public function user_add(){
        $user_db = D("User");
        $role_db = D("Role");
        if(IS_POST){
            if($model = $user_db->create())
            {
                $user_db->addUser($model);
            }else{
                $this->error($user_db->getError());
            }
        }else{
            $roles = $role_db->select();
            $this->assign("roles",$roles);
            $this->display();
        }
    }
    
    public function user_edit(){
        $user_db = D("User");
        $role_db = D("Role");
        if(IS_POST){
            if($model = $user_db->create())
            {
                $user_db->addUser($model);
            }else{
                $this->error($user_db->getError());
            }
        }else{
            $id = I("get.id",0,"intval");
            if($id){
                $the_user = $user_db->find($id);
            }else{
                $this->error("找不到此用户");
            }
            $roles = $role_db->select();
            $this->assign("the_user",$the_user);
            $this->assign("roles",$roles);
            $this->display();
        }
    }
    
    public function user_del(){
        $id = I("post.id");
        $user_db = D("User");
        $ret = $user_db->deleteUser($id);
    }
    
    public function index(){
        $user_db = D("User");
        $list = $user_db->relation(true)->select();
        foreach ($list as $key => $value) {
            $role_str = "";
            
        }
        $this->assign("list",$list);
        $this->display();
    }
    
    /*
     * 节点列表
     */
    public function permission(){
        if(IS_POST){
            $pageSize = I('post.pageSize','0','intval');
            $pageIndex = $pageSize * I('post.pageIndex','20','intval');

            $node = D("Node")->relation(true)->order('sort')->select();
            $data_count = M("Node")->count();
            $node = Tree::create($node);

            $data['data'] = $node;
            $data['recordsTotal'] = $data_count;
            $data['recordsFiltered'] = $data_count;
            $this->ajaxReturn($data);

        }else{
            $this->display();
        }

    }
    
    /*
     * 节点增加与编辑
     */
    public function permission_add(){
        $node_db = D("Node");
        if(IS_POST){
            if($model = $node_db->create())
            {
                $node_db->add();
            }else{
                $this->error($node_db->getError());
            }
        }else{
           
            
            $where['level'] = array('lt',3);
            $node = M("Node")->where($where)->order('sort')->select();
            $node = Tree::create($node);
            
            $this->assign("node",$node);
            $this->display();
        }
    }
    
    /*
     * 节点增加与编辑
     */
    public function permission_edit(){
        $node_db = D("Node");
        if(IS_POST){
            if($model = $node_db->create())
            {
                if($model['id']){
                    $node_db->save();
                }else{
                    $this->error("找不到该节点");
                }
            }else{
                $this->error($node_db->getError());
            }
        }else{
            $id = I("get.id",0,"intval");
            if($id){
                $the_node = $node_db->find($id);
            }else{
                $this->error("找不到该节点");
            }      
            $where['level'] = array('lt',3);
            $node = M("Node")->where($where)->order('sort')->select();
            $node = Tree::create($node);
            
            $this->assign("the_node",$the_node);
            $this->assign("node",$node);
            $this->display();
        }
    }
    
    /*
     * 节点删除
     */
    public function permission_del(){
        if(IS_POST){
            $ids = I("post.id");
            $id_arr = explode(",", $ids);
            $node_db = D("Node");
            $access_db = M("Access");
            foreach ($id_arr as $id) {
                $node_db->startTrans();
                $ret = $node_db->delete($id);
                if($ret){
                    $access_db->where(array("role_id" => $id))->delete();
                    $node_db->commit();
                }else{
                    $node_db->rollback();
                }
            }
            echo 1;
        }
    }
    
    
}